﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<TanHungViet.Web.Models.Product>>" %>
<%@ Import Namespace="TanHungViet.Helper"%>
<%@ Import Namespace="System.Globalization"%>

<div style="width:940px">
    <div style="float:right">
        <%= Html.ActionLink("Thêm mới", "ThemSuaSanPham", "QuanLyNoiDung", new { code = 0, contentType = ViewData["ContentType"] }, new { @class = "button" })%>
        &nbsp;|&nbsp;
        <input id="btnRemoveNews" class="button" type="button" title="Xóa sản phẩm đã chọn" onclick="removeNews()" value="Xóa"/>
    </div>
    <div style="clear:both">&nbsp;</div>

    <div>
	    <form action="" method="post" id="postlist">
            <table border="1px" cellpadding="5" cellspacing="0" width="100%">
                <tr>
                    <th width="5%" class="header"><input type="checkbox" id="check-all" name="check-all" /></th>
                    <th width="25" class="header">Tên sản phẩm</th>
                    <th width="15%" class="header">Phiên bản</th>
                    <th width="18%" class="header">Ngày cập nhật</th>
                    <th width="15%" class="header">Giá tiền</th>
                    <th width="12%" class="header">Hiện trang chủ</th>
                    <th width="10%" class="header">Thao tác</th>
                </tr>
                <tbody>
                    <% foreach (var item in Model)
                       { %>
                        <tr>
                            <td align="center">
                                <%= Html.CheckBox("NewsSelect", false, new { value = item.ProductID, @class = "selectDelete" })%>
                            </td>
                            <td><%= Html.Encode(item.ProductName)%></td>
                            <td><%= Html.Encode(item.ProductVersion)%></td>
                            <td>
                                <%= item.UpdateDate == null ? DateTime.Now.ToString("dd/MM/yyyy", new CultureInfo("vi-Vn"))
                                    : item.UpdateDate.Value.ToString("dd/MM/yyyy", new CultureInfo("vi-Vn"))%>
                            </td>
                            <td align="right">
                                <% decimal price= item.ProductPrice?? 0 ; %>
                                <%= price.ToString("##,#", new CultureInfo("vi-Vn"))%>
                            </td>
                            <td align="center">
                                <% bool inHomePage = item.InHomePage ?? false; %>
                                <%= Html.CheckBox("InHomePage", inHomePage, new { value = item.ProductID, @disabled = "disabled" })%>
                            </td>
                            <td>
                                <%= Html.ActionLink("Cập nhật", "ThemSuaSanPham", "QuanLyNoiDung", new { code = item.ProductID, contentType = ViewData["ContentType"] }, new { @class = "button" })%>
                            </td>
                        </tr>
                    <% } %>
                </tbody>
            </table>
        </form>
    </div>

    <br />

    <div>
        <div style="float:right">
        <% 
            int totalpage = Convert.ToInt32(ViewData["TotalPage"]);
            int pageindex = Convert.ToInt32(ViewData["PageIndex"]);
            int pagesize = Convert.ToInt32(ViewData["PageSize"]);
            
                
            int th = pageindex / pagesize;
            int begin = 1;
            int max = totalpage;
            if(th - 1 > 0)
            {
                begin = (th - 1) * pagesize;
            }
            if((th + 1) * pagesize < totalpage)
            {
                max = (th + 1) * pagesize;   
            }
        %>
        <% Response.Write("<input type='button' onclick='loadNewsByPage(" + 1 + ")' value='|<' />");%>
        <%    
            for(int index = begin; index <= max; index++)
            {
                if(index != pageindex)
                {
                    if((index == begin && index % pagesize == 0) || (index == max && index % pagesize == 0))
                    {
                        %>
                        <% Response.Write("<input type='button' onclick='loadNewsByPage(" + index.ToString() + ")' value='...' />"); %>
                        <%
                    }
                    else
                    {
                        %>
                        <% Response.Write("<input type='button' onclick='loadNewsByPage(" + index.ToString() + ")' value='" + index.ToString() + "' >"); %>
                        <%
                    }
                }    
                else
                {
                    %>
                    <% Response.Write("<input type='button' onclick='loadNewsByPage(" + index.ToString() + ")' value='" + index.ToString() + "' />"); %>
                    <%
                }
            }
        %>
	    <% Response.Write("<input type='button' class='dung-button' onclick='loadNewsByPage(" + (totalpage).ToString() + ")' value='>|' />");%>
        
        </div>
        <div style="clear:both">&nbsp;</div>
    </div>
</div>
<script type="text/javascript">

    function loadNewsByPage(pageindex) {
        var status = $("#statuslist").val() + "";
        if (status.indexOf("all") == -1) {
            search(pageindex);
        }
        else {
            viewAll(pageindex);
        }
    }
    
    function removeNews() {
        var listid = "";
        $("input.selectDelete:checked").each(function () {
            listid += $(this).val() + "-";
        });
        
        if (listid.length == 0) {
            alert("Vui lòng chọn một item để xóa");
        }
        else {
            if (confirm("Bạn có chắc muốn xóa không?") == true) {
                $.get("/QuanLyNoiDung/XoaSanPham/" + listid,
                    function (data) {
                        loadNewsByPage(<%= ViewData["PageIndex"] %>);
                    });
            }
        }
    }
</script>